package com.jt.www.common.service.impl;

import com.jt.www.common.service.FileService;
import com.jt.www.remote.FilesNewClient;
import feign.Response;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author xq.wu03
 * @Classname FileServiceImpl
 * @Description 文件上传下载
 * @create 2020-04-20 19:23
 */
@Service
public class FileServiceImpl implements FileService {

    public static final String FILE_URL = "/res-svc/res/v3/download?fileId=%s&applicationName=edu-svc&businessTypeCode=%s&businessDataTag=%s&free=%s";
    @Autowired
    private FilesNewClient filesNewClient;

    @Override
    public String batchupload(MultipartFile[] files, String applicationName, String businessTypeCode, String free) {
        String returnValue = filesNewClient.batchuploadV3(files, applicationName, businessTypeCode, free);
        return returnValue;

    }

    @Override
    public Response downloadFile(String fileId, String applicationName, String businessTypeCode, String businessDataTag, String free) {
        Response returnValue = filesNewClient.downloadFileV3(fileId, applicationName, businessTypeCode, businessDataTag, free);
        return returnValue;
    }

    @Override
    public Response downloadFileByFileUrl(String fileUrl) {
        if (StringUtils.isBlank(fileUrl)) {
            return null;
        }
        String[] fileUrlSplit = fileUrl.split("=|&");
        if (fileUrlSplit.length < 9) {
            return null;
        }
        String fileId = fileUrlSplit[1];
        String applicationName = fileUrlSplit[3];
        String businessTypeCode = fileUrlSplit[5];
        String businessDataTag = fileUrlSplit[7];
        String free = fileUrlSplit[9];

        return filesNewClient.downloadFileV3(fileId, applicationName, businessTypeCode, businessDataTag, free);
    }

    @Override
    public String getFileDownUrl(String fileId, String businessTypeCode, String businessDataTag, String free) {
        return String.format(FILE_URL, fileId, businessTypeCode, businessDataTag, free);
    }

}
